// functionPage/pages/minePage/checkInIfo/checkInIfo.js
import Http from '../../../../utils/http.js';
const http = new Http();
const app = getApp();

Page({
    /**
     * 页面的初始数据
     */
    data: {
        navbtn: '', //navbtn  01--操作记录进入-显示顶部导航
        id: '',
        uid: '',
        createuid: '',
        isIphone: false,
        ipxBottom: 0,
        commentTop: 101,
        realname: '',
        realnapicurl: '',
        content: '',
        city: '',
        country: '',
        sceneImageArr: [],
        description: [],
        // 当前图片索引
        currentPicIndex: 0,
        time: '',
        address: '',
        //操作弹框
        show: false,
        actions: [
            {
                name: '编辑',
                value: 'content-edit',
                color: '#333333',
            },
            {
                name: '删除',
                value: 'content-delete',
                color: '#FF455E',
            },
        ],
        commentList: [],
        commentcount: 0,
        praiseflg: '', //本人是否点赞
        praisecnt: 0, //点赞数
        //评论id
        remarkId: '', //评论id
        tocommentid: '', //被回复的评论id,回复时传
        placeHoldComment: '欢迎留言讨论', //评论/回复内容
        touid: '',
        toreplyid: '',
        privateflg: '00',
    },
    // 查看他人主页
    jumpOthersPage(e) {
        wx.navigateTo({
            url: `/functionPage/pages/othersHomePage/othersHomePage?uid=${e.currentTarget.dataset.uid}`,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let _that = this;

        wx.getSystemInfo({
            success: function (res) {
                let isIpx;
                //model中包含着设备信息
                var model = res.model;
                if (model.search('iPhone') != -1) {
                    isIpx = true;
                } else {
                    isIpx = false;
                }
                _that.setData({
                    isIphone: isIpx,
                });
            },
        });
        if (app.globalData.isIpx) {
            _that.setData({
                ipxBottom: 34,
            });
        }
        _that.setData({
            id: options.id,
            uid: options.uid,
            navbtn: options.navbtn, //navbtn  01--操作记录进入
        });
        _that.getCheckinDetail();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        this.footComment = this.selectComponent('#foot-comment');
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    change(e) {
        this.setData({
            current: e.detail.current,
        });
    },
    // 图片预览
    previewImage(e) {
        let index = e.currentTarget.dataset.index;
        this.setData(
            {
                currentPicIndex: index,
            },
            () => {
                wx.navigateTo({
                    //hideBottom隐藏图片预览的删除按钮
                    url: `../../../../pages/checkIn/imgPreview/imgPreview?hideBottom=${true}`,
                });
            }
        );
    },
    //评论
    jumpOrRemark(e) {
        if (this.data.commentTop > 100) {
            this.setData({
                toView: 'comment',
                commentTop: 0,
            });
        }
        if (e.detail.setplaceHold == '00') {
            this.footComment.articalRemark();
            this.onResetInputContent();
        }
    },

    // 重置输入框内容
    onResetInputContent() {
        this.setData({
            placeHoldComment: '欢迎留言讨论',
            tocommentid: '',
            touid: this.data.createuid,
            privateflg: '00',
        });
    },

    scrollTopFun(e) {},

    remarkInfo(e) {
        let _that = this;
        _that.footComment.remarkFocus('01');
        let name = e.detail.battleTag;
        _that.setData({
            tocommentid: e.detail.contentid || e.detail.tocommentid, //被回复的评论id,回复时传
            placeHoldComment: '@' + name, //评论/回复内容
            touid: e.detail.touid,
            toreplyid: e.detail.toreplyid,
            privateflg: e.detail.privateflg,
        });
    },
    remarkEditChange(e) {
        this.setData({
            remarkEdit: e.detail.remarkEdit, //被回复的评论id,回复时传
        });
    },
    getCheckinDetail() {
        http.getRequest(
            `cjMap/findCheckinDetail?authId=${app.globalData.authId}&id=${this.data.id}&uid=${this.data.uid}`
        ).then((res) => {
            if (res.data.type == '01') {
                wx.setNavigationBarTitle({
                    title: '打卡/工作',
                });
            } else {
                wx.setNavigationBarTitle({
                    title: '打卡/旅行路过',
                });
            }
            this.setData({
                sceneImageArr: JSON.parse(res.data.picurl),
                description: JSON.parse(res.data.picdescription),
                time: app.timeago(res.data.checkintime),
                address: res.data.cityname + '，' + res.data.country,
                commentList: res.data.commentList,
                commentcount: res.data.commentcount,
                praiseflg: res.data.praiseflg, //本人是否点赞
                praisecnt: res.data.praisecnt, //点赞数
                realname: res.data.realname,
                realnapicurl: res.data.realnapicurl,
                content: res.data.description,
                city: res.data.cityname,
                country: res.data.country,
                createuid: res.data.createuid,
            });
        });
    },
    refreshComment() {
        http.getRequest(
            `cjMap/findCheckinDetail?authId=${app.globalData.authId}&id=${this.data.id}&uid=${this.data.uid}`
        ).then((res) => {
            this.setData({
                commentList: res.data.commentList,
                commentcount: res.data.commentcount,
                praiseflg: res.data.praiseflg, //本人是否点赞
                praisecnt: res.data.praisecnt, //点赞数
            });
        });
    },
    //删除评论
    deleteRemark(e) {
        // 导航栏置灰
        wx.setNavigationBarColor({
            frontColor: '#000000',
            backgroundColor: '#484848',
            animation: {
                duration: 300,
                timingFunc: 'easeIn',
            },
        });
        this.setData({
            show: true,
            actions: [
                {
                    name: '删除',
                    value: 'remark-delete',
                    color: '#FF455E',
                },
            ],
            remarkId: e.detail.id,
        });
    },
    // 取消编辑
    onCancel() {
        // 导航栏置白
        wx.setNavigationBarColor({
            frontColor: '#000000',
            backgroundColor: '#ffffff',
            animation: {
                duration: 300,
                timingFunc: 'easeIn',
            },
        });
        this.setData({
            show: false,
        });
    },
    // 选中后
    onSelect(e) {
        console.log(e);
        let _that = this;
        if (e.detail.value == 'remark-delete') {
            wx.showModal({
                title: '提示',
                content: '确认要删除该项吗？',
                success: function (res) {
                    if (res.confirm) {
                        _that.onCancel();
                        _that.handleDeleteRemark();
                    } else {
                        _that.onCancel();
                        console.log('点击取消回调');
                    }
                },
            });
        }
    },
    handleDeleteRemark() {
        let _that = this;
        //评论删除请求
        http.getRequest(
            `cjMap/deleteComment?authId=${app.globalData.authId}&id=${_that.data.remarkId}`
        ).then((res) => {
            if (res.status == 0) {
                wx.showToast({
                    title: '删除成功',
                    icon: 'none',
                    duration: 1500,
                });
                _that.refreshComment();
            }
        });
    },
});
